requireJS(['constant', 'site', 'handlebars','public'], function (Constant, site, Handlebars,Public) {
    var url = {
        selectApplyTermList:"manager/selectApplyTermList.do",
        queryOrderBreak:"manager/queryOrderBreak.do",
        activOrder: 'manager/activOrder.do',
        selectLoanMethodList: "manager/selectLoanMethodList.do",
    }

    var order_id;
    var taskId;
    var node_id;
    var product_id;
    var s_id;

    Public.initApplyTerm();
    Public.initProductSearch();
    //渲染搜索框的贷款方式
    site.send({
        url: url.selectLoanMethodList,
        callback: function (data) {
            data = JSON.parse(data);
            if (data.type == '1') {
                var source = $("#loanMethod_list_tpl").html();
                var template = Handlebars.compile(source);
                var list = data
                var html = template(list)
                $('#loan_way').append(html);
            }
        }
    })
    var frontData = {
        pageNum: 1
    };
    // 列表
    // site.send({
    //     url: "",
    //     //async:false
    //     data: {pageNum: 1},
    //     callback: function (data) {
    //     var list = JSON.parse(data)

    // var data={"content":{"endRow":10,"firstPage":1,"hasNextPage":true,"hasPreviousPage":false,"isFirstPage":true,"isLastPage":false,"lastPage":5,"list":[{"d2_name":"业务部","sid":15,"adminName":"曾玲玲","union_name":"余德海","id_card":"412921197710185515","loan_way":1,"refuse_time":"2018-04-20 15:41:54","loan_number":"FD20180418172619870","apply_money":400000,"s_name":"已拒绝","d1_name":"宝安业务B组","oid":698,"cellphone":"13724325100","d3_name":"深圳分公司","at_name":"24个月"},{"d2_name":"深圳分公司","sid":15,"adminName":"云数","union_name":"徐万苓","id_card":"210321197204220021","loan_way":1,"refuse_time":"2018-04-20 15:41:00","loan_number":"FD20180418114242885","apply_money":200000,"s_name":"已拒绝","d1_name":"渠道部","oid":696,"cellphone":"13129525920","d3_name":"总经办","at_name":"24个月"},{"d2_name":"长沙分公司","sid":15,"adminName":"卓凯","union_name":"任栋","id_card":"422322198210180072","loan_way":1,"refuse_time":"2018-04-20 14:20:21","loan_number":"FD20180420121050285","apply_money":150000,"s_name":"已拒绝","d1_name":"渠道部","oid":701,"cellphone":"17773142962","d3_name":"总经办","at_name":"24个月"},{"d2_name":"长沙分公司","sid":15,"adminName":"天淳金融","union_name":"朱江林","id_card":"430219196809041308","loan_way":2,"refuse_time":"2018-04-20 10:32:35","loan_number":"FD20180404160520494","apply_money":100000,"s_name":"已拒绝","d1_name":"渠道部","oid":673,"cellphone":"15974178972","d3_name":"总经办","at_name":"24个月"},{"d2_name":"业务部","sid":15,"adminName":"周青萍","union_name":"田秋吉","id_card":"431224198510150375","loan_way":1,"refuse_time":"2018-04-20 10:31:19","loan_number":"FD20180330163632114","apply_money":300000,"s_name":"已拒绝","d1_name":"福田业务F2组","oid":652,"cellphone":"15817438793","d3_name":"深圳分公司","at_name":"12个月"},{"d2_name":"业务部","sid":15,"adminName":"曾玲玲","union_name":"陈开智","id_card":"522501197712045522","loan_way":1,"refuse_time":"2018-04-20 09:46:20","loan_number":"FD20180228100325376","apply_money":200000,"s_name":"已拒绝","d1_name":"宝安业务B组","oid":521,"cellphone":"18926502603","d3_name":"深圳分公司","at_name":"24个月"},{"d2_name":"深圳分公司","sid":15,"adminName":"云数","union_name":"陈一龙","id_card":"350583198409191311","loan_way":1,"refuse_time":"2018-04-20 09:46:05","loan_number":"FD20180326164055935","apply_money":200000,"s_name":"已拒绝","d1_name":"渠道部","oid":634,"cellphone":"13828736008","d3_name":"总经办","at_name":"24个月"},{"d2_name":"深圳分公司","sid":15,"adminName":"云数","union_name":"李玉清","id_card":"440923197609023809","loan_way":1,"refuse_time":"2018-04-20 09:45:51","loan_number":"FD20180404172538395","apply_money":200000,"s_name":"已拒绝","d1_name":"渠道部","oid":674,"cellphone":"13798406655","d3_name":"总经办","at_name":"24个月"},{"d2_name":"深圳分公司","sid":15,"adminName":"云数","union_name":"谭健","id_card":"452501196211120229","loan_way":1,"refuse_time":"2018-04-19 14:20:01","loan_number":"FD20180411162200771","apply_money":200000,"s_name":"已拒绝","d1_name":"渠道部","oid":686,"cellphone":"13632842713","d3_name":"总经办","at_name":"24个月"},{"d2_name":"长沙分公司","sid":15,"adminName":"天淳金融","union_name":"邹治帅","id_card":"42022219750912441X","loan_way":1,"refuse_time":"2018-04-16 17:55:00","loan_number":"FD20180329101622382","apply_money":200000,"s_name":"已拒绝","d1_name":"渠道部","oid":645,"cellphone":"18684780832","d3_name":"总经办","at_name":"24个月"}],"navigatePages":5,"navigatepageNums":[1,2,3,4,5],"nextPage":2,"orderBy":"","pageNum":1,"pageSize":10,"pages":27,"prePage":0,"size":10,"startRow":1,"total":270},"type":1,"msg":"成功"}

    // var template=Handlebars.compile($("#refuseorder_content_tpl").html())
    // var html=template(data)
    // $("#refuseorder_content").html(html)
    // }
    // })
    //日期控件
    $(".datepicker").datepicker({
        format: 'yyyy-mm-dd',
        autoclose: true
    })

    function salesManCheck() {
        var data = {
            //roleId: Constant.getAurlparam("roleId"),
            //audit_status: getAurlparam("t"),
            pageNum: frontData.pageNum,
            //susp_status: '1'
        }
        if ($("#start_time").val()) {
            data.beginTime = $("#start_time").val()
        }
        if ($("#end_time").val()) {
            data.endTime = $("#end_time").val()
        }
        if ($("#seleName option:selected").val()) {
            data.timeType = $("#seleName option:selected").val()
        }
        if ($("#seleNames option:selected").val()) {
            data.type = $("#seleNames option:selected").val()
        }
        if ($("#inputNames").val()) {
            data.keyword = $("#inputNames").val();
        }
        if ($("#at_id option:selected").val()) {
            data.atId = $("#at_id option:selected").val()
        }
        if ($("#loan_way option:selected").val()) {
            data.loan_way = $("#loan_way option:selected").val()
        }
        if($("#product_choose_list").val()){
            data.product_id = $("#product_choose_list").val()
        }
        site.send({
            url: url.queryOrderBreak,
            data: data,
            callback: function (data) {
                data = JSON.parse(data)
                if (data.type == 1) {
                    if (data.content) {
                        $('.break-page-container').createPager({
                            current: frontData.pageNum,
                            total: data.content.total,
                            pageSize: data.content.pageSize,
                            needInput: false,
                            backFn: function (current) {
                                frontData.pageNum = current;
                                salesManCheck();
                            }
                        })
                        $('.result').html(data.content.total ? data.content.total : 0);
                    }
                    //handlebars模板引擎
                    var source = $("#break_list_tpl").html()
                    var template = Handlebars.compile(source)
                    var content = data.content;

                    var menuIndex = getAurlparam('h') || 0;
                    var thirdMenusIndex = getAurlparam('t') || 0;
                    var leftIndex = getAurlparam('i') || 0;

                    var storageContent = JSON.parse(localStorage.getItem('content'));

                    var obj = {
                        content: content,
                        fourthMenus: storageContent.menus[menuIndex].children[leftIndex].children[thirdMenusIndex].children || {}
                    }
                    var html = template(obj)
                    $('#break_list').html(html)

                    //复选框一个没选中则全选去除
                    $.each($("input[name='ch_id']"), function (i, e) {
                        var flag = false;
                        $(e).on('click', function () {
                            var _checked = $("input[name='ch_id_all']")
                            var _checkedAll = $("input[name='ch_id']")
                            $.each(_checkedAll, function (ii, ee) {
                                if ($(ee).prop('checked')) {
                                    flag = true
                                }
                                else {
                                    flag = false
                                    return false;
                                }
                            })
                            _checked.prop('checked', flag);
                        })
                    })

                    //查询出委托人列表
                    function queryDelegate(current) {
                        site.send({
                            url: url.ChoiceOfclient,
                            data: {
                                pageNum: current
                            },
                            callback: function (data) {
                                data = JSON.parse(data)
                                if (data.type == 1) {
                                    var source = $("#delegate_role_list_tpl").html();
                                    var template = Handlebars.compile(source);
                                    var list = data.content
                                    var html = template(list)
                                    $('.delegate_role_list').html(html)

                                    $('.delegate-role-page-container').createPager({
                                        current: frontData.pageNum,
                                        total: data.content.total,
                                        pageSize: data.content.pageSize,
                                        needInput: false,
                                        backFn: function (current) {
                                            queryDelegate(current);
                                        }
                                    })
                                }
                                else {
                                    Constant.fn.Message("F", data.msg)
                                }
                            }
                        })
                    }


                    //渲染出模板后在进行操作
                    $(".operations").on('change', function () {
                        product_id = $(this).attr('product_id');
                        order_id = $(this).attr('order_id');
                        taskId = $(this).attr('task_id');
                        //node_type = $(this).attr('node_type')
                        node_id = $(this).attr('node_id')
                        s_id = $(this).attr('s_id')
                            if ($(this).val() == 'checkOrder') {
                            pjaxGo("application/breachOrder/detail.html?order_id=" + order_id + "&product_id=" + product_id + "&task_id=" + taskId + "&node_id=" + node_id + "&s_id=" + s_id);
                        }
                        else if ($(this).val() == 'activeOrder') {
                            $('.cover_winBox').toggle();
                            $('.coverRemarks').val('');
                        }
                        $(this).find("option:first").attr("selected", false).attr("selected", true);
                    });

                    Public.record('t_order_public');

                } else {
                    Constant.fn.Message("F",data.msg);
                }
            },
            callError: function () {
                alert("error")
            }
        })
    }

    $('.sureCover').on('click', function () {
        var remarks = $('.coverRemarks').val();
        if (!remarks) {
            Constant.fn.Message('I', '请输入备注')
            return;
        }
        site.send({
            url: url.activOrder,
            data: {
                oid: order_id,
                remarks: remarks
            },
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == 1) {
                    $('.cover_winBox').toggle();
                    Constant.fn.Message("T", data.msg);
                    pjaxReload();
                } else {
                    Constant.fn.Message("F", data.msg);
                }
            }
        })
    })


    //头部日期
    site.send({
        url: url.selectApplyTermList,
        //async:false
        data: frontData,
        callback: function (data) {
            var list = JSON.parse(data)
            var template2 = Handlebars.compile($("#refuseorder_head_date_tpl").html())
            var html2 = template2(list)
            $("#refuseorder_head_date").html(html2)
        }
    })


    salesManCheck();

    // 全选
    $(".all_checked").click(function () {
        $("input[name='ch_id']").prop('checked', $(this).prop('checked'))
    })

    //批量操作恢复订单select
    $("#allDown_select").on('change', function () {
        /*var obj = $(this).find("option:selected");
        // 下载合同
        if (obj.val() == '2') {
            allDownLoadData.oid = '';
            var inp = $("input[name='ch_id']");//获得所有check按钮
            var count = 0;
            for (var i = 0; i < inp.length; i++) {
                if (inp[i].checked) {
                    count++;
                    allDownLoadData.oid = inp[i].value;
                    allDownLoad();
                }
                if (count == 0) {
                    Constant.fn.Message("I", "未选择")
                }
            }

            $("#allDown_select").val('-1')

        }*/
        order_id = "";

        //console.log(1);
        var selected = $(this).find("option:selected");
        $(this).find("option:first").prop("selected", false).prop("selected", true);
        $("input[name='ch_id']:checked").each(function (index, item) {
            order_id = order_id + $(item).val() + ","
        })
        order_id = order_id.substring(0, order_id.length - 1)
        if (selected.val() == '恢复订单') {
            //删除产品
            if (!order_id) {
                Constant.fn.Message('I', "未选择要恢复的订单")
                return;
            }
            $('.cover_winBox').toggle();
        }
    })

    //搜索功能
    $("#search").on('click',function(){
        salesManCheck();
    })

    $(".canCover").on('click', function () {
        $(this).parents('.winBox').hide();
    })

})